package com.sm.scorpio.http;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.scorpio.baselibrary.utils.ActivityManagement;
import com.sm.scorpio.ui.bean.model.BaseBean;

import java.io.IOException;
import java.net.SocketTimeoutException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * created by zhud on 2019/8/14
 */
public class HttpNormalCallback<T extends BaseBean> implements Callback<T> {
    private String mUrl;
    private Context mContext;
    private BasicsActivityInter mActivityInter;
    //是否显示加载网络时候显示加载控件
    private boolean isShowLoadingView = false;
    //是否在加载完毕销毁控件  默认销毁
    private boolean isLoadingFinishClose = true;
    //是否显示toast信息
    private boolean isShowToast = false;

    public HttpNormalCallback(Context context) {
        this.mContext = context;
        if (mContext instanceof BasicsActivityInter) {
            mActivityInter = (BasicsActivityInter) mContext;
        }
    }

    //加载网络时候显示加载控件 true 显示 默认不显示
    public HttpNormalCallback setShowLoading(boolean isShow) {
        this.isShowLoadingView = isShow;
        if (mActivityInter != null) mActivityInter.showLoadingView();
        return this;
    }

    //加载完毕销毁控件 isClose true销毁 默认不销毁
    public HttpNormalCallback setLoadingFinishClose(boolean isClose) {
        this.isLoadingFinishClose = isClose;
        return this;
    }

    //是否显示失败的toast
    public HttpNormalCallback setShowToast(boolean isShow) {
        this.isShowToast = isShow;
        return this;
    }

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        mUrl = call.request().url().url().toString();
        Activity activity = mContext instanceof Activity ? (Activity) mContext : null;
        if (activity != null && (activity.isDestroyed() || activity.isFinishing()
                || ActivityManagement.getInstance().activityIsDestroyed(activity)))
            return;
        String message = response.message();
        Log.e(Constant.TAG, message);
        if (isShowLoadingView && isLoadingFinishClose) {
            if (mActivityInter != null) mActivityInter.closeLoadingView();
        }
        Gson gson = new GsonBuilder().serializeNulls().create();
        try {
            T data = response.body();
            gson.toJson(data);
            if (data.getCode() == Constant.SUCCEED_CODE) {
                onSuccess(data);
                onResponse(message);
                if (mActivityInter != null) mActivityInter.onNetworkLoadingSuccess(mUrl, data);
            } else if (data.getCode() == Constant.OVERDUE_CODE) {
                //过期登录信息
                onFail(data.getCode(), mUrl, data.getMsg());
                if (mActivityInter != null) {
                    mActivityInter.onNetworkLoadingFail(data.getCode(), mUrl);
                    mActivityInter.outAccToLoginActivity(mUrl);
                }
            } else {
                //请求失败
                onRequestErroor(data.getCode(), data.getMsg());
                if (isShowToast && mActivityInter != null)
                    mActivityInter.onShowToast(data.getMsg());
            }
        } catch (JsonSyntaxException e) {
            onRequestErroor(Constant.REQUEST_ERROR, mContext.getString(com.scorpio.baselibrary.R.string.string9002));
            isLoadingFinishClose = true;
        }
        complete();
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        mUrl = call.request().url().url().toString();
        if (isShowLoadingView && isLoadingFinishClose) {
            if (mActivityInter != null) mActivityInter.closeLoadingView();
        }
        String error = "";

        if (t instanceof SocketTimeoutException) {
            error = mContext.getString(com.scorpio.baselibrary.R.string.string9000);
        }
        if (t instanceof IOException) {
            error = mContext.getString(com.scorpio.baselibrary.R.string.string9001);
        }
        onRequestErroor(Constant.REQUEST_ERROR, error);
    }

    //请求失败后
    private void onRequestErroor(int code, String msg) {
        Log.e("error", msg);
        onFail(code, mUrl, msg);
        if (mActivityInter != null) {
            mActivityInter.onNetworkLoadingFail(code, mUrl);
        }
    }

    //请求失败后的 默认回调
    protected void onFail(int code, String url, String msg) {

    }

    //请求成功 返回/数据源字符
    protected void onResponse(String data) {

    }

    //请求成功 返回
    protected void onSuccess(T data) {

    }

    //请求结束
    protected void complete() {

    }
}
